# -*- coding: UTF-8 -*-
from pymysql import *
def create_table():
    conn = connect("localhost","root","123456","test05",3306)
    cur = conn.cursor()
    # cur.execute("drop table if exists students")
    #
    # sql = """create table students(
    # studentNo int primary key,
    # name varchar(10),
    # sex varchar(1),
    # hometown varchar(20),
    # age tinyint(4),
    # class varchar(20),
    # card varchar(20))"""
#     sql = """insert into students values
#     ('014', '历史', '女', '北京', '20', '1班', '340322199001247654')
# # ('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
# # ('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
# # ('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
# # ('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
# # ('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
# # ('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
# # ('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
# # ('008', '小乔', '女', '河南', '15', '3班', null),
# # ('009', '百里守约', '男', '湖南', '21', '1班', ''),
# # ('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
# # ('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
# # ('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
#     """
#     sql = "update students set name='李昭' where name='王昭君' "
#     sql = "delete  from students where studentNo='12'"
    user_inp=input("请输入：")
    # sql=("select * from students where studentNo = %s" % [user_inp])
    try:
        # 防止SQL注入用逗号（,）代替 %号

        cur.execute("select * from students where studentNo =%s" , user_inp)
        # fetchmany()括号中不写默认插一条数据，括号里写几就查几条，多出来也不会报错的
        # fetchone()查一条
        # fetchall() 查所有数据
        print(cur.fetchall())
        # conn.commit()
        # print("插入表成功")
    except Exception as e:
        print("错误原因：",e)
        conn.rollback()
    finally:
        cur.close()
        conn.close()

create_table()

"""
数据库事务的4种特性：
1.原子性：要么全部提交成功，要么全部失败回滚。
2.一致性：要么全部成功，要么全部失败。
3.隔离性，一个事务做的修改没有提交之前，对其他事务是不可见的
4.持久性：一旦提交成功会永远保存，就算此时崩溃数据也不会丢失
"""
'or 1 = 1 or'